Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localized jsx namespace #1941

Merged
merged 8 commits into from
Jul 30, 2020
Merged

Localized jsx namespace #1941

merged 8 commits into from
Jul 30, 2020

Conversation

Andarist
Copy link
Member

@Andarist Andarist commented Jul 27, 2020

fixes #1800 fixes #1257

I've discovered this magic recently and it solves, quite ideally, the problem with our types augmenting global types and thus causing conflicts with other libraries that define types for the css prop. 🎉

More info can be found here:

TODO:

  • docs
  • changeset

@changeset-bot
Copy link

changeset-bot bot commented Jul 27, 2020

🦋 Changeset is good to go

Latest commit: ad1a84d

We got this.

This PR includes changesets to release 2 packages
Name Type
@emotion/react Patch
@emotion/jest Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -0,0 +1,9 @@
import {} from 'react'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be included manually by people not wanting to use @jsx pragma. Those types here are slightly worse than the ones provided for @jsx pragma but AFAIK there is no way to override a type alias already defined in a namespace so I can't redefine LibraryManagedAttributes to only allow css prop for components already accepting className prop.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 27, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ad1a84d:

Sandbox Source
Emotion Configuration

@Andarist Andarist force-pushed the localized-jsx-namespace branch from 26bcbaa to 09afe5a Compare July 27, 2020 14:37
@Andarist Andarist force-pushed the localized-jsx-namespace branch from 57552aa to 89f8dac Compare July 27, 2020 19:06
Copy link
Member

@emmatown emmatown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome 😍 Love that we can conditionally add css rather than always

It's probably worth trying this out on some codebases to see how the type checking performance is, I've got a reasonably big one so I can try it out but others trying it would probably be good.

declare module 'react' {
interface Attributes {
css?: Interpolation<Theme>
type WithConditionalCssProp<P> = P extends { className?: string }
Copy link
Member

@emmatown emmatown Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be:

type WithConditionalCssProp<P> = "className" extends keyof P
  ? P extends { className?: string }
    ? P & { css?: Interpolation<Theme> }
    : P
  : P;

So that it doesn't add the css prop for {}

(based on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/eead2e0bc6af16ab2f2e17be652b4c337b5ffc51/types/react/index.d.ts#L805-L813)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I always forget about this surprising, to me, behavior of the {}. Good catch.

@Andarist
Copy link
Member Author

This is awesome 😍 Love that we can conditionally add css rather than always

Right! I was quite pleasantly surprised by this as well. There is one downside though - errors about css prop being missed in the props types are more cryptic. One could think that they should add it to their props instead of adding className.

It's probably worth trying this out on some codebases to see how the type checking performance is, I've got a reasonably big one so I can try it out but others trying it would probably be good.

That would be sweet! I will add the appropriate changeset and docs today/tomorrow so we'll should be able to ship new prerelease this week.

@Andarist
Copy link
Member Author

@mitchellhamilton I've added a changeset and short docs mentioning how it behaves now. Please take a look.

@Andarist Andarist merged commit f57a722 into next Jul 30, 2020
@Andarist Andarist deleted the localized-jsx-namespace branch July 30, 2020 21:11
@JakeGinnivan
Copy link
Contributor

This is cool, I will upgrade soon and see how perf is going on our project

louisgv pushed a commit to louisgv/emotion that referenced this pull request Sep 6, 2020
* Provide local JSX namespace for `jsx`

* Reuse global JSX namespace defined by React to declare our own

* Add a .d.ts file that can be included in pragma-less projects

* Fixed TSLint errors

* Fix false positive dtslint errors

* Do not allow css prop on props-less components

* add changeset

* Add short docs for css prop + TS
@github-actions github-actions bot mentioned this pull request Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants